import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { instance } from '..';

export const createRegionTypes = createAsyncThunk(
  'regionTypes/createRegionTypes',
  async (types, thunkAPI) => {
    try {
      const response = await instance.post(`/region/types`, types);
      return response;
    } catch (error) {
      return thunkAPI.rejectWithValue(error);
    }
  },
);

export const getRegionTypestAll = createAsyncThunk(
  'regionTypes/getRegioTypestAll',
  async (_, thunkAPI) => {
    try {
      const response = await instance.get("/region/types/search?from=0&to=10");
      return response;
    } catch (error) {
      return thunkAPI.rejectWithValue(error);
    }
  },
);

export const getRegionWeatherAll = createAsyncThunk(
  'regionTypes/getRegionWeatherAll',
  async (_, thunkAPI) => {
    try {
      const response = await instance.get("/region/weather/search?from=0&to=10");
      return response;
    } catch (error) {
      return thunkAPI.rejectWithValue(error);
    }
  },
);

export const updateRegionTypesById = createAsyncThunk(
  'regionTypes/putAccountById',
  async (typeId, thunkAPI) => {
    try {
      const response = await instance.put(`/region/types/${typeId}`);
      return response;
    } catch (error) {
      return thunkAPI.rejectWithValue(error);
    }
  },
);


export const deleteRegionTypesById = createAsyncThunk(
  'regionTypes/deleteAccountById',
  async (typeId, thunkAPI) => {
    try {
      const response = await instance.delete(`/region/types/${typeId}`);
      return response;
    } catch (error) {
      return thunkAPI.rejectWithValue(error);
    }
  },
);

const initialState = {
  regionTypes: [],
  isLoading: false,
  isLoading1: false,
  weathers: []
};

export const regionTypesSlice = createSlice({
  name: 'regionTypes',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(getRegionTypestAll.pending, (state, { payload }) => {
      state.isLoading = false
    });
    builder.addCase(getRegionTypestAll.fulfilled, (state, { payload }) => {
      state.isLoading = true
      state.regionTypes = payload;
    });
    builder.addCase(getRegionWeatherAll.pending, (state, { payload }) => {
      state.isLoading1 = false
    });
    builder.addCase(getRegionWeatherAll.fulfilled, (state, { payload }) => {
      state.isLoading1 = true
      state.weathers = payload;
    });
  },
});

export const {} = regionTypesSlice.actions;

export default regionTypesSlice.reducer;
